﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OneMile.DAL;
using OneMile.Entity.Model;
using OneMile.Common;
using OneMile.Entity;

namespace OneMile.BLL
{
    public class BLLUShops:BaseBLL
    {
        private DALUShops dal;
        public DALUShops DAL
        {
            get
            {
                if (dal == null)
                    dal = DALFactory.getInstance().CreateUShopsDAL();
                return dal;
            }
        }

        public IList<EShopsInfo> GetShopListByTypeLocation(string shopType, int PageNo, int PageSize, double Latitude ,double Longitude,int SortField,int SortType)
        {
            try
            {
                List<EShopsInfo> list = (List<EShopsInfo>)DAL.GetShopListByType(shopType);
                if (list == null||list.Count==0)
                {
                    return null;
                }
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].Distance = LocationUtil.GetDistance(Latitude, Longitude, list[i].Latitude, list[i].Longitude);
                }
                string PropertyName = Enum.Format(typeof(Enums.EnumShopSortField), SortField, "g");
                if (SortType == (int)Enums.EnumSortType.ASC)
                {   
                    list.Sort(p => p.GetType().GetProperty(PropertyName));
                    return list.Skip(PageSize * (PageNo - 1)).Take(PageSize).ToList<EShopsInfo>();
                }
                else
                {
                    list.SortDescending(p => p.GetType().GetProperty(PropertyName));
                    return list.Skip(PageSize * (PageNo - 1)).Take(PageSize).ToList<EShopsInfo>();
                } 
         }
            catch (Exception e)
            {

                base.WriteLog(e.Message);
                throw e;
            }
        }

        public IList<EShopsInfo> SearchShopListByNameLocation(string shopType,string shopName, int PageNo, int PageSize, double Latitude, double Longitude, int SortField, int SortType)
        {
            try
            {
                IList<EShopsInfo> list = DAL.SearchShopListByName(shopName,shopType);
                if (list == null || list.Count == 0)
                {
                    return null;
                }
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].Distance = LocationUtil.GetDistance(Latitude, Longitude, list[i].Latitude, list[i].Longitude);
                }
                string PropertyName = Enum.Format(typeof(Enums.EnumShopSortField), SortField, "g");
                if (SortType == (int)Enums.EnumSortType.ASC)
                {
                    return list.OrderBy(p => p.GetType().GetProperty(PropertyName)).Skip(PageSize * (PageNo - 1)).Take(PageSize).ToList<EShopsInfo>();
                }
                else
                {
                    return list.OrderByDescending(p => p.GetType().GetProperty(PropertyName)).Skip(PageSize * (PageNo - 1)).Take(PageSize).ToList<EShopsInfo>();
                } 
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public IList<EShopsInfo> GetShopListByType(string shopType, int PageNo, int PageSize, int SortField, int SortType)
        {
            try
            {
                return DAL.GetShopListByType(shopType, PageNo, PageSize, SortType, SortField);
            }
            catch (Exception e)
            {

                base.WriteLog(e.Message);
                throw e;
            }
        }


        public IList<EShopsInfo> SearchShopListByName(string shopName, int PageNo, int PageSize, int SortField, int SortType)
        {
            try
            {
                return DAL.SearchShopListByName(shopName, PageNo, PageSize, SortType, SortField);
            }
            catch (Exception e)
            {
                
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public byte[] GetBigShopImgByShopId(string ShopId)
        {
            try
            {
                return DAL.GetBigShopImgByShopId(ShopId);
            }
            catch (Exception e)
            {
                
                base.WriteLog(e.Message);
                throw e;
            }
        }
    }
}
